#include "Tank.h"
#include<QMatrix>
#include<QPainter>
#include<ctime>
#include<QDebug>

Tank::Tank(const Tank& obj): DynamicObject(obj, true)
{
    id = obj.id;
    nick = obj.nick;
    action = obj.action;
    health = obj.health;
    max_health = obj.max_health;
}

Tank::Tank(const Tank &obj, bool copyScene): DynamicObject(obj, copyScene)
{
    m_posX = obj.m_posX;
    m_posY = obj.m_posY;
    setZValue(obj.zValue());
}

Tank& Tank::operator = (const Tank & obj)
{
    DynamicObject::operator = (obj);
    m_posX = obj.m_posX;
    m_posY = obj.m_posY;
    id = obj.id;
    action = obj.action;
    nick = obj.nick;
    max_health = obj.max_health;
    health = obj.health;
    setZValue(obj.zValue());
    return (*this);
}

Tank::Tank(int PosX, int PosY, char Direction, int speed, double progres, int ID, int max_health): DynamicObject(PosX,PosY, Direction,speed, progres), id(ID)
{
    health = max_health ;
    this->max_health = max_health;
    action = 'S';
    m_ptr_bitmap = new QPixmap(":/Gameitems/tank.png");
    setZValue(-2);
}

bool Tank::advanceAction(long duration)
{
    if (action != 'S')
    {
        m_progress += static_cast<double>(duration) / CLOCKS_PER_SEC * m_speed * 100;

        if (action == 'M')
        {
            if (m_progress >= 100.0)
            {
                m_progress -= 100.0;
                switch (m_direction)
                {
                case 'L':
                    m_posX--;
                    break;
                case 'R':
                    m_posX++;
                    break;
                case 'U':
                    m_posY--;
                    break;
                default:
                    m_posY++;
                }
                return true;
            }
        }
        else
        {
            if (m_progress >= 100.0)
            {
                m_progress -= 100.0;
                switch (m_direction)
                {
                case 'L':
                    m_direction = (action == 'L' ? 'D' : 'U');
                    break;
                case 'R':
                    m_direction = (action == 'R' ? 'D' : 'U');
                    break;
                case 'U':
                    m_direction = (action == 'L' ? 'L' : 'R');
                    break;
                default:
                    m_direction = (action == 'R' ? 'L' : 'R');
                }
                return true;
            }
        }
    }

    return false;
}

void Tank::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, QWidget *widget)
{
    /*int iDir;
    switch (action)
    {
    case 'L':
        iDir = 1;
        break;
    case 'R':
        iDir = 3;
        break;
    case 'U':
        iDir = 0;
        break;
    case 'D':
        iDir = 2;
        break;
    }
    int iDirCur;
    if(m_direction!= iDir)
    {
        switch(m_direction)
        {
        case 'U':
            iDirCur = 0;
            break;
        case 'L':
            iDirCur = 1;
            break;
        case 'D':
            iDirCur = 2;
            break;
        case 'R':
            iDirCur = 3;
            break;
        }
        if(iDirCur > iDir)
            this->rotate(-90);
        else
            this->rotate(+90);
    }
*/
    qDebug()<<"in tank direction "<<m_direction;
    int scale =25;

    this->setPos(m_posX, m_posY);
    painter->drawPixmap(0,0,scale*4,scale*4,*m_ptr_bitmap);
    //painter->drawText(this->m_posX, this->m_posY,m_ptr_bitmap->width(),m_ptr_bitmap->height(),0x0044, nick);
}

int Tank::GetID()
{
   return id;
}

int Tank::GetHealth()
{
   return health;
}

QString Tank::GetNick()
{
    return nick;
}

char Tank::GetCurrentAction()
{
    return action;
}

void Tank::SetID(int ID)
{
     id = ID;
}

void Tank::SetNick(QString Nick)
{
    nick = Nick;
}

void Tank::SetHealth(int Health)
{
    health = Health;
}

void Tank::SetCurrentAction(char Action)
{
    action = Action;
}


